package com.vision.sales.sv.report;

import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;

import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import ar.com.fdvs.dj.core.DynamicJasperHelper;
import ar.com.fdvs.dj.core.layout.ClassicLayoutManager;
import ar.com.fdvs.dj.core.layout.LayoutManager;
import ar.com.fdvs.dj.domain.DynamicReport;
import ar.com.fdvs.dj.domain.Style;
import ar.com.fdvs.dj.domain.builders.ColumnBuilderException;
import ar.com.fdvs.dj.domain.builders.FastReportBuilder;
import ar.com.fdvs.dj.domain.constants.Border;
import ar.com.fdvs.dj.domain.constants.GroupLayout;
import ar.com.fdvs.dj.domain.constants.HorizontalAlign;
import ar.com.fdvs.dj.domain.constants.VerticalAlign;
import ar.com.fdvs.dj.domain.entities.DJGroup;
import ar.com.fdvs.dj.domain.entities.columns.PropertyColumn;

import com.vision.sales.cm.db.data.TransactionEntry;

public class PrintTest {
	
	public static void main(String[] args) throws IOException, ColumnBuilderException, ClassNotFoundException, JRException {
		final int MARGINS = 30;
		
		Style subtitleStyle = new Style("style.subtitle");
		subtitleStyle.setTransparent(true);
		subtitleStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		subtitleStyle.setHorizontalAlign(HorizontalAlign.CENTER);
		subtitleStyle.setTextColor(Color.BLACK);
		
		Style headerStyle = new Style("style.header");
		headerStyle.setTransparent(true);
		headerStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		headerStyle.setHorizontalAlign(HorizontalAlign.LEFT);
		headerStyle.setTextColor(Color.BLACK);
		headerStyle.setBorderBottom(Border.PEN_1_POINT());
		
		Style amountHeaderStyle = new Style("style.amountheader");
		amountHeaderStyle.setTransparent(true);
		amountHeaderStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		amountHeaderStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
		amountHeaderStyle.setBorderBottom(Border.PEN_1_POINT());
		
		Style detailStyle = new Style("style.detail");
		detailStyle.setTransparent(true);
		detailStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		detailStyle.setHorizontalAlign(HorizontalAlign.LEFT);
		detailStyle.setTextColor(Color.BLACK);
		
		Style amountDetailStyle = new Style("style.amountdetail", detailStyle.getName());
		amountDetailStyle.setHorizontalAlign(HorizontalAlign.RIGHT);
		
		Style groupStyle = new Style("style.group");
		groupStyle.setVerticalAlign(VerticalAlign.MIDDLE);
		groupStyle.setBorderBottom(Border.PEN_1_POINT());
		groupStyle.setPaddingTop(5);
		groupStyle.getFont().setBold(true);
		
		// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
		
		FastReportBuilder f = new FastReportBuilder();
		
		f.setTitle("Customer Ledger");
		f.setSubtitle("11/25/2011 to 11/25/2012");
		f.setSubtitleStyle(subtitleStyle);
		
		f.setWhenNoData("There is no data to display.", null, true, true);
		f.setUseFullPageWidth(true);
		f.setMargins(MARGINS, MARGINS, MARGINS, MARGINS);
		
		f.addColumn("Customer", "customer", String.class, 200, detailStyle, headerStyle);
		
		f.addColumn("Date", "date", Date.class, 100, detailStyle, headerStyle);
		f.getColumn(1).setPattern("MM/dd/yyyy");
		
		f.addColumn("Transaction", "transaction", String.class, 100, detailStyle, headerStyle);
		
		f.addColumn("Amount", "amount", Double.class, 80, amountDetailStyle, amountHeaderStyle);
		f.getColumn(3).setPattern("#,##0.00");
		
		DJGroup g = new DJGroup();
		g.setColumnToGroupBy((PropertyColumn) f.getColumn(0));
		g.setLayout(GroupLayout.VALUE_IN_HEADER);
//		g.setLayout(new GroupLayout(true, false, false, true, false));
		f.getColumn(0).setStyle(groupStyle);
		f.addGroup(g);
		
		DynamicReport dr = f.build();
		LayoutManager layout = new ClassicLayoutManager();
		JRDataSource dataSource = getDataSource();
		JasperPrint print = DynamicJasperHelper.generateJasperPrint(dr, layout, dataSource);
		
		JRAbstractExporter exporter = new JRPdfExporter();
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);

		// Set the output stream
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);

		// Export the report
		exporter.exportReport();

		// Set the content-length
		byte[] reportAsBytes = baos.toByteArray();

		// Write to the response's output stream
		OutputStream os = new FileOutputStream("D:\\customer_ledger.pdf");
		try {
			os.write(reportAsBytes, 0, reportAsBytes.length);
		} catch (IOException e) {
			e.printStackTrace();
		}
		os.flush();
		os.close();
	}
	
	public static JRDataSource getDataSource() {
		ArrayList<TransactionEntry> list = new ArrayList<TransactionEntry>();
		String[] transactions = { "Sales Invoice", "Collection", "Advance Payment", "DM - Overruns", "CM - Discounts", "CM - Rejects", "CM - Returns", "CM - Bad Debts" };
		
		for (int i = 0; i < 100; i++) {
			
		}
		
		Collections.sort(list, new Comparator<TransactionEntry>() {
			@Override
			public int compare(TransactionEntry arg0, TransactionEntry arg1) {
				int diff = arg0.getCustomer().getCode().compareToIgnoreCase(arg1.getCustomer().getCode());
				if (diff != 0) return diff;
				return arg0.getDate().compareTo(arg1.getDate());
			}
		});
		
		return new JRBeanCollectionDataSource(list);
	}

}
